/*
 * Copyright 2012 Marsrutai.lt
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package lt.marsrutai.mobile.android.manager;

import lt.marsrutai.mobile.android.AppContext;
import android.app.Activity;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceClickListener;

/**
 * Contains common preference screen preparation methods
 *
 * @author Justas
 *
 */
public class CachePreferenceManager extends AppPreferenceManager {

    public final static String KEY_UPDATE_REQUEST_CACHE = "update_request_cache";
    public final static String KEY_CLEAN_REQUEST_CACHE = "clean_request_cache";
    public final static String KEY_CURRENT_REQUEST_CACHE_SIZE = "current_request_cache_size";

    public CachePreferenceManager(Activity activity, Object caller) {
        super(activity, caller);
    }

    /**
     * Registers listeners for various preferences
     *
     * @param caller
     */
    public void registerListeners() {
        Preference updateRequestCache = findPreference(KEY_UPDATE_REQUEST_CACHE);
        updateRequestCache.setOnPreferenceClickListener(new OnPreferenceClickListener() {

            @Override
            public boolean onPreferenceClick(Preference preference) {
                AppContext.getCacheUpdateManager().checkForCacheUpdates(activity, true);
                return true;
            }

        });

        Preference cleanRequestCache = findPreference(KEY_CLEAN_REQUEST_CACHE);
        cleanRequestCache.setOnPreferenceClickListener(new OnPreferenceClickListener() {

            @Override
            public boolean onPreferenceClick(Preference preference) {
                AppContext.getCacheUpdateManager().showAllDataDeleteConfirmationDialog(activity);
                return true;
            }

        });
    }

    /**
     * Updates the summary of "current request cache size" preference
     */
    public void updateCurrentRequestCacheSize() {
        Preference currentRequestCacheSize = findPreference(KEY_CURRENT_REQUEST_CACHE_SIZE);
        currentRequestCacheSize.setSummary(String.format("%.2f MB", AppContext.getDataCacheController().getCacheSize() / (1024.0 * 1024.0)));
    }
}
